/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TTTWindow.java
 *
 * Created on 26.08.2009, 23:44:53
 */

package jtplugin;

/**
 *
 * @author christoph
 */
public class TTTWindow extends javax.swing.JFrame {
    
    public String symbols[] = {"", "X", "O"};
    
    private static int ENEMY_REMOTE = 0;
    private static int ENEMY_AI = 1;
    private boolean userBegins = true;

    private int userSymbol = 1;
    private int enemyType = ENEMY_AI;
    private int aIQuality = 0;
    private String enemyName;

    private TTTGameLogic myGameLogic = null;
    private TicTacToe myPlugin;
    
    /** Creates new form TTTWindow */
    public TTTWindow(TicTacToe _myPlugin) {
        this.myPlugin = _myPlugin;
        initComponents();        
    }

    public boolean setExternalAction(int _index) {
        return this.myGameLogic.externalAction(_index);
    }

    public void startNewGame(boolean _userBegins, int _userSymbol, int _enemyType, int _aIQuality, String _enemyName) {
        System.out.println("----> in TTTWindow.startNewGame()");

        for(int i=1; i<10; i++) {
            try {
                ((javax.swing.JButton)(this.getClass().getDeclaredField("jButton"+i).get(this))).setEnabled(true);
            } catch (Exception _e) {
            }
        }
        
        this.userBegins = _userBegins;
        this.userSymbol = _userSymbol;
        this.enemyType = _enemyType;
        this.aIQuality = _aIQuality;
        this.enemyName = _enemyName;

        if(this.myGameLogic == null)
            this.myGameLogic = new TTTGameLogic(this);

        this.jLabelPlayer1Info.setText(this.myPlugin.myName +  ": " + this.myPlugin.winCounterUser);
        this.jLabelPlayer2Info.setText(this.myPlugin.enemyName +  ": " + this.myPlugin.winCounterEnemy);
        
        this.myGameLogic.startGame(this.userBegins, this.userSymbol, this.enemyType, this.aIQuality);
    }

    public void gameEnds(int _winner) {
        if(_winner == 0) {
            // User wins
            this.myPlugin.winCounterUser++;
            this.markWinCombi(this.myGameLogic.lastWinCombi);
        } else if(_winner == 1) {
            // enemy wins
            this.myPlugin.winCounterEnemy++;
            this.markWinCombi(this.myGameLogic.lastWinCombi);
        }

        this.jLabelPlayer1Info.setText(this.myPlugin.myName +  ": " + this.myPlugin.winCounterUser);
        this.jLabelPlayer2Info.setText(this.myPlugin.enemyName +  ": " + this.myPlugin.winCounterEnemy);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel4 = new javax.swing.JPanel();
        jLabelPlayer1Info = new javax.swing.JLabel();
        jLabelPlayer2Info = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jButtonRestart = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabelStatus = new javax.swing.JLabel();

        setTitle("Tic-Tac-Toe Plugin");
        setMinimumSize(new java.awt.Dimension(250, 250));

        jPanel4.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel4.setLayout(new java.awt.GridLayout(2, 1));

        jLabelPlayer1Info.setText("Player1: 0");
        jPanel4.add(jLabelPlayer1Info);

        jLabelPlayer2Info.setText("Player2: 0");
        jPanel4.add(jLabelPlayer2Info);

        getContentPane().add(jPanel4, java.awt.BorderLayout.EAST);

        jButtonRestart.setText("Restart");
        jButtonRestart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRestartActionPerformed(evt);
            }
        });
        jPanel3.add(jButtonRestart);

        jButtonCancel.setLabel("Cancel");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });
        jPanel3.add(jButtonCancel);

        getContentPane().add(jPanel3, java.awt.BorderLayout.SOUTH);

        jPanel1.setLayout(new java.awt.GridLayout(3, 3));

        jButton1.setFont(new java.awt.Font("Dialog", 1, 14));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);

        jButton2.setFont(new java.awt.Font("Dialog", 1, 14));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2);

        jButton3.setFont(new java.awt.Font("Dialog", 1, 14));
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton3);

        jButton4.setFont(new java.awt.Font("Dialog", 1, 14));
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton4);

        jButton5.setFont(new java.awt.Font("Dialog", 1, 14));
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton5);

        jButton6.setFont(new java.awt.Font("Dialog", 1, 14));
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton6);

        jButton7.setFont(new java.awt.Font("Dialog", 1, 14));
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton7);

        jButton8.setFont(new java.awt.Font("Dialog", 1, 14));
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton8);

        jButton9.setFont(new java.awt.Font("Dialog", 1, 14));
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton9);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jLabelStatus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jtplugin/ttticon.png"))); // NOI18N
        jLabelStatus.setText("Waiting...");
        jPanel2.add(jLabelStatus, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel2, java.awt.BorderLayout.NORTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(this.myGameLogic.userAction(1))
            this.myPlugin.setTurn(1);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        // Cancel clicked
           this.jLabelStatus.setText("Cancelling game...");
           this.myPlugin.endCurrentGame();
}//GEN-LAST:event_jButtonCancelActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if(this.myGameLogic.userAction(2))
            this.myPlugin.setTurn(2);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        if(this.myGameLogic.userAction(3))
            this.myPlugin.setTurn(3);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        if(this.myGameLogic.userAction(4))
            this.myPlugin.setTurn(4);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        if(this.myGameLogic.userAction(5))
            this.myPlugin.setTurn(5);
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        if(this.myGameLogic.userAction(6))
            this.myPlugin.setTurn(6);
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        if(this.myGameLogic.userAction(7))
            this.myPlugin.setTurn(7);
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        if(this.myGameLogic.userAction(8))
            this.myPlugin.setTurn(8);
    }//GEN-LAST:event_jButton8ActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        if(this.myGameLogic.userAction(9))
            this.myPlugin.setTurn(9);
    }//GEN-LAST:event_jButton9ActionPerformed

    private void jButtonRestartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRestartActionPerformed
        if(this.enemyType == ENEMY_AI)
            this.restartGame();
        else {            
            this.myPlugin.startNewGame(true);
        }
    }//GEN-LAST:event_jButtonRestartActionPerformed

    public void setStatusString(String _status) {
        this.jLabelStatus.setText(_status.replaceAll("LOCAL", this.myPlugin.myName).replaceAll("ENEMY", this.myPlugin.enemyName));
    }

    public void setSymbolOnButton(int _button, int _symbol) {
        try {
            ((javax.swing.JButton)(this.getClass().getDeclaredField("jButton"+_button).get(this))).setText(this.symbols[_symbol]);
        } catch (Exception _e) {
        }
    }

    public void markWinCombi(String _combi) {
        int buttonNum = 0;
        for(int i=0; i<3; i++) {
            buttonNum = Integer.parseInt(_combi.substring(i,i+1));
            try {
                ((javax.swing.JButton)(this.getClass().getDeclaredField("jButton"+buttonNum).get(this))).setEnabled(false);
            } catch (Exception _e) {
            }
        }
    }

    public void restartGame() {
        for(int i=1; i<10; i++) {
            try {
                ((javax.swing.JButton)(this.getClass().getDeclaredField("jButton"+i).get(this))).setEnabled(true);
            } catch (Exception _e) {
            }
        }

        this.userBegins = !this.userBegins;
        this.myGameLogic.startGame(this.userBegins, this.userSymbol, this.enemyType, this.aIQuality);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonRestart;
    private javax.swing.JLabel jLabelPlayer1Info;
    private javax.swing.JLabel jLabelPlayer2Info;
    private javax.swing.JLabel jLabelStatus;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    // End of variables declaration//GEN-END:variables

}
